<?php
// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------
namespace app\model\api;

use laytp\BaseModel;
use think\model\concern\SoftDelete;

class SkyTask extends BaseModel
{
    use SoftDelete;
    protected $name = 'sky_task';

    /**
     * 今日任务
     * @param $date
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function todayTask($date){
        $season = (new SkyCategory())->where('category','=','季节先祖')
            ->cache(true)
            ->column('sub_category');
        $list = $this->where(array('date' => $date))->order('sort desc')->select()->toArray();
        if(empty($list)){
            return [];
        }
        $task = [];
        foreach($list as $item){
            $target = '';
            $type = '';
            $target_season = '';

            if(!empty($item['target'])){
                $target = $item['target'];
                $type = 1;
            }else{
                if(!empty($item['target_season'])){
                    $target_season = $item['target_season'];
                    if(in_array($item['target_season'],$season)){
                        $type = 2;
                    }else{
                        $type = 3;
                    }

                }
            }

            $taskDesc = json_decode($item['task_desc'],true);
            $taskDesc = is_array($taskDesc)?$taskDesc:str_replace(PHP_EOL,'</br>',$item['task_desc']);

            $task[] = array(
                'id' => $item['id'],
                'name' => $item['task_name'],
                'icon' => $item['icon'],
                'intro' => array(
                    'spiritID'     => $target,
                    'spiritSeason' => $target_season,
                    'spiritType'   => $type,
                    'title'        => $item['task_name'],
                    'desc'         => $taskDesc,
                    'images'       => json_decode($item['task_img'],true),
                )
            );
        }
        return $task;
    }

}